<?php
class apibusiness_response_order_mogujie_b2cv1 extends  apibusiness_response_order_mogujie_abstract{
    
    /**
     * 是否接收(除活动订单外的其他订单)
     *
     * @return void
     * @author
     **/
    protected function accept_dead_order(){
        $rs = parent::accept_dead_order();
        #订单取消的，先放过
        if ($rs == false && $this->_ordersdf['status'] == 'dead') {
            unset($this->_apiLog['info']['msg']);
            return true;
        }
        return $rs;
    }
    /*     
     * 是否接收订单
    *
    * @return void
    * @author
    **/
    protected function canAccept(){
        $result = parent::canAccept();
        if ($result === false) {
            return false;
        }
    
        # 未支付的款到发货订单拒收
        if ($this->_ordersdf['shipping']['is_cod'] != 'true' && $this->_ordersdf['pay_status'] == '0') {
            $this->_apiLog['info']['msg'] = '未支付订单不接收';
            return false;
        }
        if($this->_ordersdf['ship_status'] == '1'){
            $this->_apiLog['info']['msg'] = '已发货订单不接收';
            return false;
        }
    
        return true;
    }    
            
    /**
     * �ܹ���������
     *
     * @return void
     * @author
     **/
    public function canCreate(){
        if ($this->_ordersdf['status'] != 'active') {
            $this->_apiLog['info']['msg'] = ($this->_ordersdf['status'] == 'dead') ? '取消的订单不接收' : '完成的订单不接收';
            return false;
        }
        #蘑菇街创建订单的时候，未支付订单不接受
        if($this->_ordersdf['pay_status'] != '1'){
            $this->_apiLog['info']['msg'] =  '未支付蘑菇街订单不接收';
            return false;
        }
    }      

    /**
     * �������
     *
     * @return void
     * @author
     **/
    protected function canUpdate(){
        $rs = parent::canUpdate();
        
        if( !$rs){
            return false;
        }
        $orderModel =     app::get(self::_APP_NAME)->model('orders');
        $obj_refund_apply =     app::get(self::_APP_NAME)->model('refund_apply');
        $oOperation_log = app::get(self::_APP_NAME)->model('operation_log');
        
        if($this->_ordersdf['pay_status'] != '1'){
            #所有退款申请单状态，属于已拒绝，才可以接受本次更新
            $refund_info = $obj_refund_apply->getlist('order_id,status',array('order_id'=>$this->_tgOrder['order_id'],'status|noequal'=>3));
            if(!empty($refund_info)){
                $this->_apiLog['info'][] = '返回值：系统已存在退款，不再更新';
                return false;
            }
        }
        #需要检查是否存在退款申请
        
        switch($this->_ordersdf['pay_status']){
            case 0;
                if($this->_ordersdf['status'] == 'dead'){
                    #取消ERP活跃、未发货的订单
                    if ($this->_tgOrder['status'] == 'active' && $this->_tgOrder['ship_status'] == '0') {
                        $ordersdf = array('payed' => '0','pay_status' => '5');
                        $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                        $memo = '前端已退款，取消订单';
                        $orderModel->cancel($this->_tgOrder['order_id'],$memo,false,'async');
                        $this->_apiLog['info'][] = '返回值：订单退款取消成功';
                    }
                }
            break;
             case 1;
                if($this->_ordersdf['status'] == 'active' && $this->_ordersdf['ship_status'] == '0'){
                    #前端已经拒绝退款的，将退款中状态还原
                    if ($this->_tgOrder['status'] == 'active' && $this->_tgOrder['pay_status'] == '6') {
                        #还得再检查本地是否已经存在退款
                        $refund_info = $obj_refund_apply->getlist('order_id,status',array('order_id'=>$this->_tgOrder['order_id'],'status|noequal'=>3));#3是已拒绝
                        if(!empty($refund_info)){
                            $this->_apiLog['info'][] = '返回值：系统已存在退款，无法更新';
                            return false;
                        }
                        $ordersdf = array('pay_status' => '1','pause'=>'false');
                        $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                        $memo ='前端已拒绝退款，系统将订单状态自动还原';
                        $oOperation_log->write_log('order_modify@ome',$this->_tgOrder['order_id'],$memo);
                        $this->_apiLog['info'][] = '返回值：订单更新成功';
                    }
                }
            break;
            #部分退款
            case 4:
                if($this->_tgOrder['status'] == 'active' && $this->_tgOrder['ship_status'] == '0') {
                    $ordersdf = array('payed' =>$this->_ordersdf['payed'],'pay_status' => '4','pause'=>'true');
                    $rs =  $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                    $memo ='前端有退款，更新支付状态为部分退款';
                    $oOperation_log->write_log('order_modify@ome',$this->_tgOrder['order_id'],$memo);
                    $this->_apiLog['info'][] = '返回值：订单修改成功';
                }
                break;
           #全额退款
            case 5:
                if($this->_tgOrder['status'] == 'active' && $this->_tgOrder['ship_status'] == '0') {
                    $ordersdf = array('payed' => '0','pay_status' => '5');
                    $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                    $memo = '前端已退款，取消订单';
                    $orderModel->cancel($this->_tgOrder['order_id'],$memo,false,'async');
                    $this->_apiLog['info'][] = '返回值：订单退款取消成功';                    
                }
                break;
            #退款申请中
            case 6:
                #已支付的,才可以改为退款申请中
                if($this->_tgOrder['status'] == 'active' && $this->_tgOrder['ship_status'] == '0' && $this->_tgOrder['pay_status'] == '1') {
                    $ordersdf = array('pay_status' => '6','pause'=>'true');
                    $orderModel->update($ordersdf,array('order_id' => $this->_tgOrder['order_id']));
                    $this->_apiLog['info'][] = '返回值：订单修改成功';
                    $memo ='前端申请退款，更新支付状态为退款申请中';
                    $oOperation_log->write_log('order_modify@ome',$this->_tgOrder['order_id'],$memo);
                }
                break;
        }        
        
        return $rs;
    }
    /**
     * 获取更新信息插件
     *
     * @return void
     * @author
     **/
     public function get_update_plugins()
    {
        $plugins = parent::get_update_plugins();
        $plugins[] = 'mogujie';
        return $plugins;
    }   
}